// app.js
App({
  onLaunch() {
    // 展示本地存储能力
    const logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    const that = this
    // 登录
    wx.login({
      success: res => {
        wx.request({
          url: 'https://thinker.skyseaee.com/login', 
          method:'POST',
          data: {
            code:res.code
          }, 
          header: {
            'content-type': 'application/x-www-form-urlencoded' // 默认值
          },
          success (res) {
            //console.log(res.data)
            that.globalData.userid = res.data.userid
            wx.setStorage({
              data: res.data.userid,
              key: 'userid',
            })
          }
        })
      }
    })
    // 检查登录
    let avatar = wx.getStorageSync('avatar') || ''
    if(avatar.length > 0) {
      this.globalData.isLogin = true
      let userid = wx.getStorageSync('userid')
      this.globalData.userid = userid
      let musics = wx.getStorageSync('musics') || []
      if(musics.length===0) {
        wx.request({
          url: 'https://thinker.skyseaee.com/getPortfolio',
          data:{
            userid:userid
          },
          success(res){
            let portfolio = res.data.portfolio
            let musicIDList = []
            console.log(portfolio)
            for(let port of portfolio) {
              musicIDList.push(port.orderID)
            }
            that.globalData.musics = musicIDList
            wx.setStorage({
              data:musicIDList,
              key:'musics'
            })
          }
        })
      } else {
        this.globalData.musics = musics
      }
    }
    let orderID = wx.getStorageSync('lastMusicId') || ''
    if(orderID.length > 0) {
      this.globalData.hasSource = 1
      this.currentMusicId = orderID
    }

    wx.getSystemInfo({
      success: function (res) {
        that.globalData.windowHeight = res.windowHeight
      }
    })

  },  
  globalData: {
    userInfo: null,
    updateStatus:0,
    hasSource:0,
    currentMusicId:'',
    isLogin:false,
    currentAudio:null,
    curCover:'',
    windowHeight:0,
    userid:-1,
    musics:[]
  }
})
